// Copyright 2010, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.adwords.lib.utils;

import com.google.api.adwords.lib.AdWordsUser;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * A utility class for retrieving information about API usage.
 *
 * @author api.arogal@gmail.com (Adam Rogal)
 */
public class UnitsUtils {
  private final AdWordsUser user;

  /**
   * Constructs a {@code UnitsUtils} class for the {@link AdWordsUser} that
   * the class will perform operations on.
   *
   * @param user the root {@code AdWordsUser} to sum all children from when
   *     calculating units usage, or to use to gather info on method usage.
   */
  public UnitsUtils(AdWordsUser user) {
    this.user = user;
  }

  /**
   * Returns a {@code Map<String, Long>} mapping of "service"."method" to the
   * number of units used. Running this example will consume 71 quota units.
   * It should be noted that this data is not in real time and is refreshed
   * every few hours.
   *
   * @param startDate the start date
   * @param endDate the end date
   * @return a {@code Map<String, Long>} mapping of "service"."method" to the
   *     number of units used
   * @throws Exception temporary exception while support is
   *     being built
   */
  public Map<String, Long> getMethodUsage(Date startDate, Date endDate)
      throws Exception {
    // TODO(api.arogal): Implement when ready.
    throw new Exception("Not supported in v2009 as of yet.");
  }

  /**
   * Gets the quota usage per child of the entire account tree below the root
   * user. That is, for each child that is a client manager, all units below
   * that client manager are summed upwards. The result is very useful
   * for invoicing sub-MCCs that may have many clients that units may be spent
   * on.
   *
   * @param startDate the start date
   * @param endDate the end date
   * @return a {@code ClientQuotaUsageMap} representing the client quota usage
   * @throws Exception temporary exception while support is
   *     being built
   */
  public UnitsUsageMap getClientUnitsUsage(Date startDate, Date endDate)
      throws Exception {
    // TODO(api.arogal): Implement when ready.
    throw new Exception("Not supported in v2009 as of yet.");
  }

  /**
   * Gets the quota usage for a user by summing its own usage with the usage
   * of its children. If a child is an MCC, it is recursively summed.
   *
   * @param user the root of the current sub-tree to sum
   * @param startDate the start date
   * @param endDate the end date
   * @param unitsUsageMap the map to populate the client to usage data with
   * @param clientEmailToManagers a map of client to list of managers
   * @param doubleCountedChildren a list of doubly counted children
   * @return a summation of the root node's usage and its children's usage
   * @throws Exception temporary exception while support is
   *     being built
   */
  private static long getClientUnitsUsageHelper(AdWordsUser user, Date startDate,
      Date endDate, Map<String, Long> unitsUsageMap, Map<String,
      List<String>> clientEmailToManagers, List<String> doubleCountedChildren)
      throws Exception {
    // TODO(api.arogal): Implement when ready.
    throw new Exception("Not supported in v2009 as of yet.");
  }
}
